package cn.t.converter.order;


import cn.t.core.converter.CoreDTOConverter;
import cn.t.dto.device.CargowaySkuDTO;
import cn.t.dto.device.PickUpCompleteDTO;
import cn.t.dto.device.vo.OpenCloseDTO;
import cn.t.dto.device.vo.QtyVO;
import cn.t.dto.order.ExceptionLogDTO;
import cn.t.enums.OptionTypeEnum;
import cn.t.enums.order.ErrorTypeEnum;
import cn.t.model.device.DeviceDataDO;
import cn.t.model.order.ExceptionLogDO;
import cn.t.model.order.OrderDO;
import cn.t.vo.andriod.AndroidDeviceVO;
import cn.t.vo.user.FirmVO;
import cn.t.vo.user.ParticipantVO;
import cn.t.vo.user.UserSessionVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.ObjectUtils;

import java.util.Map;
import java.util.Optional;

/**
 * 设备异常日志记录表(ExceptionLog)dto转domain
 *
 * @author t
 * @since 2024-05-24 10:19:34
 */
@Mapper(componentModel = "spring")
public interface ExceptionLogDTOConverter extends CoreDTOConverter<ExceptionLogDTO, ExceptionLogDO> {
    @Mapping(target = "stdWeight", source = "cargowaySkuDTO.skuWeight")
    @Mapping(target = "deviceId", source = "model.deviceId")
    @Mapping(target = "id",ignore = true)
    @Mapping(target = "gmtCreated", ignore = true)
    @Mapping(target = "gmtModified", ignore = true)
    @Mapping(target = "creator", ignore = true)
    @Mapping(target = "modifier", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    ExceptionLogDO  do2Do(OpenCloseDTO model, CargowaySkuDTO cargowaySkuDTO);
    ExceptionLogDO  modelDo2Do(OpenCloseDTO model);
    default ExceptionLogDO setErrDo(PickUpCompleteDTO model, AndroidDeviceVO device, UserSessionVO session, ErrorTypeEnum errorTypeEnum){
        ExceptionLogDO exceptionLogDO =new ExceptionLogDO();
        //参与者信息
        Optional<FirmVO> first = session.getFirms().stream().filter(firmVO -> firmVO.getCustomerId().equals(device.getFirmId())).findFirst();
        if (first.isPresent()) {
            Optional<ParticipantVO> first1 = first.get().getParticipants().stream().findFirst();
            first1.ifPresent(participantVO -> {
                exceptionLogDO.setParticipantId(participantVO.getParticipantId());
            });
        }

        exceptionLogDO.setBusinessType(OptionTypeEnum.order_open.getCode());
        exceptionLogDO.setOrderSn(model.getOrderSn());
        exceptionLogDO.setUserId(session.getUserId());
        exceptionLogDO.setUserName(session.getIndividualVO().getCustomerName());
        exceptionLogDO.setFirmId(device.getFirmId());
        exceptionLogDO.setDeviceId(device.getDeviceId());
        exceptionLogDO.setDeviceName(device.getDeviceName());
        exceptionLogDO.setExceptionType(errorTypeEnum.getCode());
        exceptionLogDO.setRemark(model.getRemark());
        return exceptionLogDO;
    }
    default ExceptionLogDO getExceptionLogDO(OpenCloseDTO openCloseDO, CargowaySkuDTO cargowaySkuDTO, QtyVO qtyVO) {
        ExceptionLogDO exceptionLogDO = do2Do(openCloseDO, cargowaySkuDTO);


        exceptionLogDO.setBusinessType(openCloseDO.getOptionTypeEnum().getCode());
        Map<Integer, Double> openScale = openCloseDO.getOpenScale();
        if (!ObjectUtils.isEmpty(openScale.get(qtyVO.getNo()))) {
            exceptionLogDO.setBeforeWeight(openScale.get(qtyVO.getNo()));
        }
        Map<Integer, Double> scaleMap = openCloseDO.getScaleMap();
        if (!ObjectUtils.isEmpty(scaleMap.get(qtyVO.getNo()))) {
            exceptionLogDO.setAfterWeight(scaleMap.get(qtyVO.getNo()));
        }
        exceptionLogDO.setCargowayNo(qtyVO.getNo());
        exceptionLogDO.setExceptionType(qtyVO.getErrorTypeEnum().getCode());
        exceptionLogDO.setRemark(qtyVO.getErrorTypeEnum().getName());
        exceptionLogDO.setCreator(openCloseDO.getOperatorInfo());
        exceptionLogDO.setModifier(openCloseDO.getOperatorInfo());
        return exceptionLogDO;
    }
    @Mapping(target = "stdWeight", source = "cargowaySkuDTO.skuWeight")
    @Mapping(target = "deviceId", source = "model.deviceId")
    @Mapping(target = "gmtCreatedStart", ignore = true)
    @Mapping(target = "gmtCreatedEnd", ignore = true)
    @Mapping(target = "gmtCreated", ignore = true)
    @Mapping(target = "gmtModified", ignore = true)
    @Mapping(target = "creator", ignore = true)
    @Mapping(target = "modifier", ignore = true)
    @Mapping(target = "gmtModifiedStart", ignore = true)
    @Mapping(target = "gmtModifiedEnd", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    @Mapping(target = "id",ignore = true)
    ExceptionLogDO  add2Do(DeviceDataDO model, CargowaySkuDTO cargowaySkuDTO);

    default ExceptionLogDO pickUpdo2Exception(OrderDO orderDO, CargowaySkuDTO cargowaySkuDTO, ErrorTypeEnum errorTypeEnum) {

        ExceptionLogDO exceptionLogDO = new ExceptionLogDO();
        exceptionLogDO.setBusinessType(OptionTypeEnum.order_open.getCode());
        exceptionLogDO.setOrderSn(orderDO.getOrderSn());
        exceptionLogDO.setUserId(orderDO.getUserId());
        exceptionLogDO.setUserName(orderDO.getUserName());
        exceptionLogDO.setParticipantId(orderDO.getParticipantId());
        exceptionLogDO.setFirmId(orderDO.getFirmId());
        exceptionLogDO.setDeviceId(orderDO.getDeviceId());
        exceptionLogDO.setDeviceName(orderDO.getDeviceName());
        exceptionLogDO.setCargowayId(cargowaySkuDTO.getCargowayId());
        exceptionLogDO.setSkuCode(cargowaySkuDTO.getSkuCode());
        exceptionLogDO.setSkuImg(cargowaySkuDTO.getSkuImg());
        exceptionLogDO.setSkuName(cargowaySkuDTO.getSkuName());
        exceptionLogDO.setExceptionType(errorTypeEnum.getCode());
        exceptionLogDO.setRemark(errorTypeEnum.getName());
        exceptionLogDO.setCategoryName(cargowaySkuDTO.getCategoryName());
        exceptionLogDO.setCategoryId(Long.valueOf(cargowaySkuDTO.getCategoryId()));
        exceptionLogDO.setStdWeight(cargowaySkuDTO.getSkuWeight().doubleValue());
        exceptionLogDO.setCargowayNo(cargowaySkuDTO.getCargowayNo());


        return exceptionLogDO;
    }

}

